package cn.shopthrive.merchants;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.HashMap;

import io.flutter.embedding.android.FlutterActivity;

public class VivoCustomNoticeActivity extends FlutterActivity {
  private static final String TAG = "VivoCustomNoticeActivity";

  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    handleIntent(getIntent());
  }

  @Override
  protected void onNewIntent(@NonNull Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    handleIntent(intent);
  }

  private void handleIntent(Intent intent) {
    if (intent == null) {
      return;
    }
    long messageId = intent.getLongExtra("vivo_push_messageId", -1);

    Bundle extras = intent.getExtras();
    String route = "/tabs/orders";
    String skipContent = null;
    int skipType = -1;
    HashMap<String, Object> params = new HashMap<>();
    if (extras != null) {
      for (String key : extras.keySet()) {
        if (!TextUtils.isEmpty(key)) {
          Object value = extras.get(key);
          String content = value == null ? "null" : value.toString();
          if ("route".equals(key)) {
            route = content;
          } else if ("skipContent".equals(key)) {
            skipContent = content;
          } else if ("skipType".equals(key)) {
            try {
              skipType = Integer.parseInt(content);
            } catch (NumberFormatException e) {
            }
          } else if (!"vivo_push_messageId".equals(key)) {
            params.put(key, content);
          }
        }
      }
    }

    MyApplication app = (MyApplication) getApplication();
    if (skipType == 2 && skipContent != null && skipContent.startsWith("http")) {
      app.openUrl(this, skipContent);
    } else {
      HashMap<String, Object> data = new HashMap<>();
      data.put("route", route);
      data.put("params", params);
      data.put("skipType", skipType);
      app.notifyPushMessage(this, data);
    }
  }
}